<?php
class editAction extends rareAction{
	public function execute(){
		$id = $this->getGetParam('id',0);
		if($id==0) forward('category/index');
		$cateDao = new DaoCategory();
		$cate = $cateDao->getOne("id='$id'");
		
		$dao = new DaoImage();
		$images = $dao->getOne("itemID='$id' and typeID='".service_type::CATEGORY."'");
		if(!empty($images)){
			$this->assign('images',array($images));
		}

		$this->assign('cate',$cate);
	}
	public function executePost(){
		$id = $this->getPostParam('id',0);
		$type = $this->getPostParam('type',service_type::PRODUCT);
		rare_go404If($id==0);
		$cate = $this->getPostParam('cate',array());
		if(empty($cate)) jsonReturn(0,'null');
		
		$images = $this->getPostParam('img',array());
		if(!empty($images)){
			$dao = new DaoImage();
			$imgs = $dao->getOne("itemID='$id' and typeID='".service_type::CATEGORY."'");
			if(empty($imgs)){
				service_image::insert($images,service_type::CATEGORY,$id);
			}else{
				$dao->update("id='".$imgs['id']."'",array('src'=>$images[0]));
			}
		}

		$cateDao = new DaoCategory();
		$parent = $cateDao->getOne('id='.$cate['parentID']);
		if(empty($parent)){
			$cate['path'] = "0,$id";
		}else{
			$cate['path'] = $parent['path'].",$id";
		}
		$cate['title'] = trim($cate['title']);
		$cate['description'] = trim($cate['description']);
		$cateDao->update("id='$id'",$cate);
		jsonReturn(1,$type);
	}
}
?>
